package com.cks.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cks.eduservice.entity.EduSubject;
import com.cks.eduservice.entity.excel.SubjectData;
import com.cks.eduservice.entity.subject.OneSubject;
import com.cks.eduservice.entity.subject.TwoSubject;
import com.cks.eduservice.listener.subjectExcelListener;
import com.cks.eduservice.mapper.EduSubjectMapper;
import com.cks.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.nio.cs.ext.EUC_CN;

import javax.security.auth.Subject;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-16
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

@Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new subjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
           // e.printStackTrace();
        }
    }

    //课程分类列表 树形
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
      //查询所有一级分类
        QueryWrapper<EduSubject>  wrapper =new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapper);
        //查询所有二级分类
        QueryWrapper<EduSubject>  wrapper2 =new QueryWrapper<>();
        wrapper2.ne("parent_id",0);
        List<EduSubject> twoSubjects = baseMapper.selectList(wrapper2);

        //创建list集合用于存储最终封装的数据
       List<OneSubject> list =new ArrayList<>();
       //  封装一级分类
        for (int i = 0; i < oneSubjects.size(); i++) {
            EduSubject eduSubject = oneSubjects.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject,oneSubject);//与上面的二行等同
            list.add(oneSubject);

            //在一级分类循环遍历查询所有的二级分类
            ArrayList<TwoSubject> twoSubjects1 = new ArrayList<>();
            for (int j = 0; j < twoSubjects.size(); j++) {
                EduSubject eduSubject1 = twoSubjects.get(j);
                if(eduSubject.getId().equals(eduSubject1.getParentId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject1,twoSubject);
                    twoSubjects1.add(twoSubject);
                }
            }
            //把一级下面所有二级分类放到一级分类里面
           oneSubject.setChildren(twoSubjects1);
        }


        return list;
    }
}
